/**
 * Created by GYL on 2018/7/25
 */
import React, { Component } from 'react';
import { Tabs, Tag, Checkbox, Select, message, Modal } from 'antd';
import style from './GasStationCallCom.less';
import FiltrateUpgrade from '../../../components/common/FiltrateUpgrade';
import Container from '../../../components/common/Container';
import MyTable from '../../../components/common/MyTable';
import PublicService from '../../../services/PublicService';
import MyModal from '../../../components/common/MyModal';
import AddModal from './sub/AddModal';
import CheckModal from './sub/CheckModal';
import request from '../../../utils/request';
import MyPagination from '../../../components/common/MyPagination';
import moment from 'moment';
const TabPane = Tabs.TabPane;
const Option = Select.Option;
const confirm = Modal.confirm;
var dataSource1 = [

]
class GasStationCallCom extends Component {

    constructor(props) {
        super(props)


    }

    state = {
        checkModaldata:{},
        tagshow:true,
        addModal: false,
        checkModal: false,
        modalBtnLoading: false,
        page: 1,
        rows: 10,
        total: 0,
        btn: "查看",
        customerName: "瓶装气供应站(点)安全检查考核评分表",
        kcType: "",
        status: "",
        EditId: "",
        totalArr: [],
        bottomArr: [],
        footerArr: [],
        moddleArr: [],
        headerArr: [],
        loading: false,
        selectedRowKeys: [],
        selectedRows: [],
        dataSource: [],
        pageIndex: 1,
        pageSize: 10,
        reportName:"pzqgyz",
        columns: [
            {
                title: '序号',
                dataIndex: 'num',
                width: '5%',
                align: "center",
            },
            {
                title: '检查考核时间',
                dataIndex: 'takeTime',
                width: '15%',
                align: "center",
                render: (text, record) => <span>{text}<span style={{ width: 5, height: 20, backgroundColor: record.status == 1 ? "#52c41a" : "#fc1808", position: "absolute", left: 0 }}></span></span>
            },
            {
                title: "报表名称",
                dataIndex: 'reportChineseName',
                width: '20%',
                align: "center",
                render: (text) => <span>{text ? text : '/'}</span>
            },
            {
                title: '被检查单位',
                width: '20%',
                dataIndex: 'supplystation',
                align: "center",
            },
            {
                title: '检查考核人',
                dataIndex: 'assessor',
                width: '15%',
                align: "center",
            },
            {
                title: '得分',
                dataIndex: 'score',
                width: '8%',
                align: "center",
            },

            {
                title: '操作',
                dataIndex: 'userName',
                width: '17%',
                align: "center",
                render: (text, record) => {
                    return <span>
                        {/* <Tag onClick={this.onCheck1.bind(this, record)}>查看</Tag> */}
                        <Tag color={record.status == 1 ? "#108ee9" : "#ccc"} onClick={this.onCheck.bind(this, record)}>查看</Tag>
                        <Tag color={record.status === 0 ? "#87d068" : "#ccc"} onClick={this.showConfirm.bind(this, record)}>短信提醒</Tag>
                    </span>

                }
            },

        ]
    }

    //短信提醒
    showConfirm = (record) => {
        console.log(record)
        if (record.status == 1) {
            return
        }

        if (record === '一键') {
            confirm({
                centered: true,
                style: { top: 200, left: "70vh" },
                title: `提醒`,
                content: `发送短信至所有负责人?`,
                onOk() {
                    return new Promise((resolve, reject) => {
                        setTimeout(Math.random() > 0.5 ? resolve : reject, 1000);
                        message.success("发送成功")
                    }).catch(() => console.log('Oops errors!'));
                },
                onCancel() { },
            });
        } else {
            confirm({
                centered: true,
                style: { top: 200, left: "70vh" },
                title: `提醒`,
                content: `发送短信至负责人?`,
                onOk() {
                    return new Promise((resolve, reject) => {
                        setTimeout(Math.random() > 0.5 ? resolve : reject, 1000);
                        message.success("发送成功")
                    }).catch(() => console.log('Oops errors!'));
                },
                onCancel() { },
            });
        }





    }

    //提交
    onSubmit = (page, size, val) => {
        console.log(page, size, val)
        this.setState({
            loading: true,
            columns:[],
            dataSource:[]
        })

        if(this.state.reportName === 'pzqgyz') {
            let data = {
                customerName: val && val.customerName ? val.customerName : "",
                supplystation: val && val.supplystation ? val.supplystation : "",
                takeTime: val && val.takeTime ? moment(val.takeTime).format("YYYY-MM-DD") : "",
                assessor: val && val.assessor ? val.assessor : "",
    
                reportName: this.state.reportName,
                type: 2,
                page, rows: size,
                status: this.state.status
            }
            request({
                url: '/zhrq-web/report/common/getPageList',
                method: 'POST',
                data: data
            }).then(data => {
                if (data.rc == 0) {
                    data.ret.items.forEach((item) => {
                        item.takeTime = moment(item.takeTime).format("YYYY-MM-DD HH:mm")
                    })
                }
                console.log(data)
                this.setState({
                    columns: [
                        {
                            title: '序号',
                            dataIndex: 'num',
                            width: '5%',
                            align: "center",
                        },
                        {
                            title: '检查考核时间',
                            dataIndex: 'takeTime',
                            width: '15%',
                            align: "center",
                            render: (text, record) => <span>{text}<span style={{ width: 5, height: 20, backgroundColor: record.status == 1 ? "#52c41a" : "#fc1808", position: "absolute", left: 0 }}></span></span>
                        },
                        {
                            title: "报表名称",
                            dataIndex: 'reportChineseName',
                            width: '22%',
                            align: "center",
                            render: (text) => <span>{text ? text : '/'}</span>
                        },
                        {
                            title: '被检查单位',
                            width: '20%',
                            dataIndex: 'supplystation',
                            align: "center",
                        },
                        {
                            title: '检查考核人',
                            dataIndex: 'assessor',
                            width: '15%',
                            align: "center",
                        },
                        {
                            title: '得分',
                            dataIndex: 'score',
                            width: '8%',
                            align: "center",
                        },
            
                        {
                            title: '操作',
                            dataIndex: 'userName',
                            width: '17%',
                            align: "center",
                            render: (text, record) => {
                                return <span>
                                    {/* <Tag onClick={this.onCheck1.bind(this, record)}>查看</Tag> */}
                                    <Tag color={record.status == 1 ? "#108ee9" : "#ccc"} onClick={this.onCheck.bind(this, record)}>查看</Tag>
                                    <Tag color={record.status === 0 ? "#87d068" : "#ccc"} onClick={this.showConfirm.bind(this, record)}>短信提醒</Tag>
                                </span>
            
                            }
                        },
            
                    ],
                    dataSource: data.ret.items,
                    total: data.ret.rowCount,
                    tagshow:true
                })
                setTimeout(() => {
                    this.setState({
                        loading: false
                    })
                }, 500)
            })
        }

        if(this.state.reportName === "rhaqjcb") {
            let data = {
                customerName: val && val.customerName ? val.customerName : "",
                supplystation: val && val.supplystation ? val.supplystation : "",
                takeTime: val && val.takeTime ? moment(val.takeTime).format("YYYY-MM-DD") : "",
                assessor: val && val.assessor ? val.assessor : "",
    
                reportName: this.state.reportName,
                type: 2,
                page, rows: size,
                status: this.state.status
            }
            request({
                url: '/zhrq-web/report/arriveChecklist/getPageList',
                method: 'POST',
                data: data
            }).then(data => {
                if (data.rc == 0) {
                    data.ret.items.forEach((item) => {
                        item.takeTime = moment(item.takeTime).format("YYYY-MM-DD HH:mm")
                    })
                }
                console.log(data)
                this.setState({
                    columns: [
                        {
                            title: '序号',
                            dataIndex: 'num',
                            width: '5%',
                            align: "center",
                        },
                        {
                            title: '检查时间',
                            dataIndex: 'takeTime',
                            width: '15%',
                            align: "center",
                        },
                        {
                            title: "报表名称",
                            dataIndex: 'reportName',
                            width: '20%',
                            align: "center",
                            render: (text) => <span>{text ? text : '/'}</span>
                        },
                        {
                            title: "报表编号",
                            dataIndex: 'reportNumber',
                            width: '10%',
                            align: "center",
                            render: (text) => <span>{text ? text : '/'}</span>
                        },
                        {
                            title: '燃气站点',
                            width: '20%',
                            dataIndex: 'supplystation',
                            align: "center",
                        },
                        {
                            title: '检查人',
                            dataIndex: 'assessor',
                            width: '15%',
                            align: "center",
                        },
                        {
                            title: '操作',
                            dataIndex: 'userName',
                            width: '17%',
                            align: "center",
                            render: (text, record) => {
                                return <span>
                                    <Tag onClick={this.onCheck1.bind(this, record)}>查看</Tag>
                                </span>
            
                            }
                        },
            
                    ],
                    dataSource: data.ret.items,
                    total: data.ret.rowCount,
                    tagshow:false
                })
                setTimeout(() => {
                    this.setState({
                        loading: false
                    })
                }, 500)
            })
        }


    };

    componentDidMount() {

        this.onSubmit(1, 10)
    }


    //删除按钮
    setDelConfirm = () => {
        if (this.state.selectedRows.length < 1) {
            message.error('请选择删除对象!');
            return
        }



        let data = []
        this.state.selectedRows.forEach((item) => {
            data.push({ id: item.id })
        })
        console.log(this.state.selectedRows, data)
        request({
            url: '/hh-management-web/hehui/baseuser/delete',
            method: 'POST',
            data
        }).then(data => {
            if (data.rc == 0) {
                this.setState({
                    selectedRowKeys: []
                })
                message.success(`删除成功!`);
                //调用分页查询
                this.onSubmit(1, 10);
            }
        })


    }


    onSelectChange = (keys, rows) => {
        this.setState({
            selectedRowKeys: keys,
            selectedRows: rows
        });
    };

    onTag = (v) => {
        this.setState({
            status: v
        })

        setTimeout(() => {
            this.onSubmit(1, 10)
        }, );
    }

    //查看按钮
    onCheck = (record) => {
        if (record.status === 0) {
            message.warning("未自检状态,不可查看!")
            return
        }
        this.setState({
            EditId: record.reportId
        })

        if (record.reportName == "pzqgyz") {
            this.setState({
                customerName: "瓶装气供应站(点)安全检查考核评分表"
            })
            request({
                url: `/zhrq-web/report/bottledgasSupplystationScore/getBottledgasSupplystationScore?id=${record.reportId}`,
                method: 'GET',

            }).then(data => {
                console.log(data)
                if (data.rc == 0) {
                    data.ret.bottledgasSupplystationScoreInformationDtoList.forEach((item, index) => {
                        item.paramName = item.assessId
                        item.initialValue = item.score
                        item.disabled = true
                    })
                    data.ret.bottledgasSupplystationScoreVetoItemDtoList.forEach((item, index) => {
                        item.paramName = item.itemVetoId
                        item.initialValue = item.veto
                        item.disabled = true
                    })
                    if (data.ret.score === 0) {
                        data.ret.score = "0"
                    }

                    this.setState({
                        totalArr: [
                            {
                                name: "总分",
                                paramName: "score",
                                key: "1",
                                initialValue: data.ret.score,
                                disabled: true
                            }
                        ],
                        headerArr: [
                            {
                                name: "被检供应站名称",
                                paramName: "supplystation",
                                key: "1",
                                initialValue: data.ret.supplystation,
                                disabled: true
                            },
                            {
                                name: "被检查人",
                                paramName: "beInspector",
                                key: "2",
                                initialValue: data.ret.beInspector,
                                disabled: true
                            },


                        ],
                        bottomArr: [
                            {
                                type: 'input',
                                name: "检查考核人",
                                paramName: "assessor",
                                key: "1",
                                initialValue: data.ret.assessor,
                                disabled: true
                            },
                            {
                                type: "datePicker",
                                name: "检查考核时间",
                                paramName: "takeTime",
                                key: "2",
                                initialValue: moment(data.ret.takeTime),
                                disabled: true
                            }
                        ],
                        moddleArr: data.ret.bottledgasSupplystationScoreInformationDtoList,
                        footerArr: data.ret.bottledgasSupplystationScoreVetoItemDtoList,
                        addModal: true
                    })
                }
            })
        }
    }

    onCheck1 = (record) => {
        request({
            url: '/zhrq-web/report/arriveChecklist/findOneById',
            method: 'POST',
            data:{id:record.id}
        }).then(data => {
            console.log(data)
            if(data.rc == 0) {
                this.setState({
                    checkModaldata:data.ret,
                    checkModal: true
                })
            }
        })
       
       
    }
    ModalCancel = () => {
        this.setState({
            addModal: false,
            checkModal: false
        })
    }

    ModalOk = () => {
        this.setState({
            addModal: false,
            checkModal: false
        })
    }


    //分页器
    onChangePage = (page, size) => {
        this.onSubmit(page, size);
        this.setState({
            page,
            rows: size,
            pageIndex: page,
            pageSize: size
        })
    };

    //下拉改变
    onSelectedChange = (fun, nextParams, paramName, nextParams2, value) => {
        console.log(fun)
        // this.setState({
        //     customerName: fun.customerName
        // })
        if(fun.customerName === "入户安全检查表") {
            this.setState({
                reportName:"rhaqjcb"
            })
        }

        if(fun.customerName === "瓶装气供应站(点)安全检查考核评分表") {
            this.setState({
                reportName:"pzqgyz"
            })
        }

        

    }

    render() {

        const { total, addModal, loading, selectedRowKeys, dataSource, pageIndex, pageSize, columns, checkModal } = this.state

        const items = [
            {
                type: 'select',
                label: '报表名称',
                paramName: 'customerName',
                placeholder: '请输入',
                style: { width: 300 },
                options: [
                    { text: '瓶装气供应站(点)安全检查考核评分表', value: '瓶装气供应站(点)安全检查考核评分表' },
                    { text: '入户安全检查表', value: '入户安全检查表' },
                ]
            },

            {
                type: 'input',
                label: "燃气站点",
                paramName: 'supplystation',
                placeholder: '请输入',
                width: 200
            },

            {
                type: 'datePicker',
                label: '检查时间',
                paramName: 'takeTime',
                placeholder: '请输入',
                width: 200
            },

            {
                type: 'input',
                label: '检查人',
                paramName: 'assessor',
                placeholder: '请输入',
                width: 200
            },
        ];

        let rowSelection = {
            selectedRowKeys,
            onChange: this.onSelectChange,
        };

        const windowWidth = document.body.clientWidth;

        const height = document.body.clientHeight;
        let containerHeight = '75vh';
        if (windowWidth > 1367) {
            containerHeight = '77vh';
        }

        return (
            <div>
                {
                    addModal &&
                    <AddModal
                        // type={type}
                        visible={addModal}
                        title={" "}
                        footerTitle={this.state.btn}
                        // modalBtnLoading={modalBtnLoading}
                        footerShow={true}
                        onModalSave={this.ModalOk}
                        onCancel={this.ModalCancel.bind(this, 'addModal')}
                        customerName={this.state.customerName}
                        headerArr={this.state.headerArr}
                        moddleArr={this.state.moddleArr}
                        footerArr={this.state.footerArr}
                        bottomArr={this.state.bottomArr}
                        totalArr={this.state.totalArr}
                    />
                }
                {
                    checkModal &&
                    <CheckModal
                        // type={type}
                        visible={checkModal}
                        title={" "}
                        footerTitle={"关闭"}
                        // modalBtnLoading={modalBtnLoading}
                        footerShow={true}
                        onModalSave={this.ModalOk}
                        onCancel={this.ModalCancel.bind(this, 'checkModal')}
                        data={this.state.checkModaldata}
                    />
                }
                <FiltrateUpgrade
                    items={items}
                    style={{ width: windowWidth > 1900 ? '71%' : '65%' }}
                    ref={ref => { this.f1 = ref }}
                    submit={this.onSubmit.bind(this, 1, 10)}
                    selectedChange={this.onSelectedChange}
                // btnRight={true}
                // height={windowWidth > 1900 ? 136 : 126}
                />
                <Container
                    style={{ height: containerHeight, position: "relative" }}
                    exportBtnShow={true}
                    exportBtn={this.exportBtn}
                // addBtnText={this.addBtnText}
                // examineBtn={this.examineBtn}
                // editBtn={this.editBtn}
                // lockBtn={this.lockBtn}
                // clearlockBtn={this.clearlockBtn}
                // setDelConfirm={this.setDelConfirm}
                // addBtnShow={true}
                // deleteBtnShow={true}
                // editBtnShow={true}
                // examineBtnShow={true}
                // lockBtnShow={true}
                // clearlockBtnShow={true}
                >
                {
                    this.state.tagshow &&
                    <div className={style.Con_header}>
                    <Tag color="#f56a27" onClick={this.showConfirm.bind(this, "一键")}>一键提醒</Tag>

                    <Tag color={this.state.status === "" ? "#108ee9" : "blue"} onClick={this.onTag.bind(this, "")}>全部</Tag>
                    <Tag color={this.state.status === 0 ? "#fc1808" : "red"} onClick={this.onTag.bind(this, 0)}>未自检</Tag>
                    <Tag color={this.state.status == 1 ? "#52c41a" : "green"} onClick={this.onTag.bind(this, 1)}>已自检</Tag>

                    <span style={{ float: "right" }}>
                        提醒时间
                        <Select defaultValue="16时" style={{ width: 120, margin: '0 10px' }} size="small">
                            <Option value="15时">15时</Option>
                            <Option value="16时">16时</Option>

                        </Select>
                    </span>
                    <Checkbox style={{ float: "right" }}>开启自动提醒</Checkbox>

                </div>

                }
               

                    <div style={{ height: "62vh", overflow: "auto" }}>
                        <MyTable
                            // bordered
                            loading={loading}
                            columns={columns}
                            // rowSelection={rowSelection}
                            pagination={false}
                            // scroll={{ y: windowWidth < 1400 ? height - 280 : height - 300 }}
                            dataSource={PublicService.transformArrayData(dataSource, true, true, pageIndex, pageSize)}
                        />

                    </div>
                    <MyPagination
                        showPage
                        pageSize={pageSize}
                        current={pageIndex}
                        total={total}
                        showSizeChanger
                        showQuickJumper
                        onChange={this.onChangePage}
                        onShowSizeChange={this.onChangePage}
                    />
                </Container>


            </div>
        )
    }
}
export default (GasStationCallCom);
